#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main() {
  int i,j ,k;
  int N;
  cin >> N;
  vector<int> vec;
  vec.push_back(0);
  bool zero = true;
  for (i = 0; i < N ; ++i) { cin >> k; if(k) zero = false; vec.push_back(k); }
  if (zero) cout << 0 << endl;
  else if (N < 3) cout << -1 << endl;
  else {
    int res = 0;
    for (i = N; i > 0; i--) {
      if (vec[i] < 0) continue;
      while (vec[i] > 0) {
        if (i & 1) {
          if (i >= 3) {
            vec[i]--;
            vec[i-1]--;
            vec[i/2]--;
          } else vec[i]--;
          res++;
        } else {
          if (i == N) {
            cout << -1 << endl;
            return 0;
          }
          vec[i]--;
          vec[i/2]--;
          res++;
        }
      }
    }
    cout << res << endl;
  }

}
